package com.hemerasolutions.v2trainer.model.users;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.examination.Exam;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.model.schedule.Schedule;
import com.hemerasolutions.v2trainer.model.exercise.Equipment;

@Entity
public class Athlete extends BaseModel  {
 
	private static final long serialVersionUID = 1L;
	 
    @Column(length = 20)
	private String firstName;

    @Column(length = 30)
	private String lastName;
	 
    @Column(length = 14)
    private String cpf;
	
    @Column(length = 50)
    private String address;

    @Column(length = 30)
	private String state;

    @Column(length = 30)
	private String city;

    @Column(length = 9)
	private String postalCode;	
	
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date birthday;
	 
    @Column
	private boolean maritalStatus;
	
    @Column
	private boolean male;
	
    @Column
    private int level;
	
    @Column
    private int recoveryFactor;

    @OneToOne(cascade=CascadeType.ALL)
    private User user;
    
    @OneToOne(cascade=CascadeType.ALL,orphanRemoval=true)
    private Photo photo;    

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	private List<Schedule> schedules;

    @OneToMany
	private List<Equipment> equipments;

    @OneToMany(cascade=CascadeType.ALL,mappedBy = "athlete")
	private List<Exam> exams;
    
    @ManyToMany
    private List<Academy> academys;
    
    @ManyToMany
    private List<PersonalTrainer> personalTrainers;

	public Athlete() {
		super();

		this.user 				= new User();
		this.equipments 		= new ArrayList<Equipment>();
		this.schedules 			= new ArrayList<Schedule>();
		this.exams				= new ArrayList<Exam>();
		this.personalTrainers   = new ArrayList<PersonalTrainer>();
		this.academys			= new ArrayList<Academy>();
		
		this.user.setUserType(User.USERTYPE_ATHLETE);
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getAddress() {
		return address;
	}

	public String getState() {
		return state;
	}

	public String getCity() {
		return city;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public boolean isMaritalStatus() {
		return maritalStatus;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setState(String state) {
		this.state = state;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public void setMaritalStatus(boolean maritalStatus) {
		this.maritalStatus = maritalStatus;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public boolean isMale() {
		return male;
	}

	public void setMale(boolean male) {
		this.male = male;
	}

	public String getCpf() {
		return cpf;
	}

	public void setCpf(String cpf) {
		this.cpf = cpf;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public List<Exam> getExams() {
		return exams;
	}

	public void setiExams(List<Exam> Exams) {
		this.exams = Exams;
	}

	public List<Equipment> getEquipments() {
		return equipments;
	}

	public void setEquipments(List<Equipment> equipments) {
		this.equipments = equipments;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public boolean isMarried() {
		return maritalStatus;
	}

	public void setMarried(boolean married) {
		this.maritalStatus = married;
	}

	public int getRecoveryFactor() {
		return recoveryFactor;
	}

	public void setRecoveryFactor(int recoveryFactor) {
		this.recoveryFactor = recoveryFactor;
	}

	public List<Schedule> getSchedules() {
		return schedules;
	}

	public void setSchedules(List<Schedule> schedules) {
		this.schedules = schedules;
	}


	public void setExams(List<Exam> exams) {
		this.exams = exams;
	}

	

	public List<Academy> getAcademys() {
		return academys;
	}


	public List<PersonalTrainer> getPersonalTrainers() {
		return personalTrainers;
	}


	public void setAcademys(List<Academy> academys) {
		this.academys = academys;
	}


	public void setPersonalTrainers(List<PersonalTrainer> personalTrainers) {
		this.personalTrainers = personalTrainers;
	}

	
	public List<Equipment> getSharedEquipments(){
		List<Equipment> shared = new ArrayList<Equipment>();
			
		for(PersonalTrainer pt:this.getPersonalTrainers()){
			shared.addAll(pt.getEquipments());
		}
		
		for(Academy academy:this.getAcademys()){
			shared.addAll(academy.getEquipments());
		}
		
		return shared;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result
				+ ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((city == null) ? 0 : city.hashCode());
		result = prime * result + ((cpf == null) ? 0 : cpf.hashCode());
		result = prime * result
				+ ((equipments == null) ? 0 : equipments.hashCode());
		result = prime * result + ((exams == null) ? 0 : exams.hashCode());
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result + level;
		result = prime * result + (male ? 1231 : 1237);
		result = prime * result + (maritalStatus ? 1231 : 1237);
		result = prime * result + ((photo == null) ? 0 : photo.hashCode());
		result = prime * result
				+ ((postalCode == null) ? 0 : postalCode.hashCode());
		result = prime * result + recoveryFactor;
		result = prime * result
				+ ((schedules == null) ? 0 : schedules.hashCode());
		result = prime * result + ((state == null) ? 0 : state.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Athlete other = (Athlete) obj;
		if (address == null) {
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (city == null) {
			if (other.city != null)
				return false;
		} else if (!city.equals(other.city))
			return false;
		if (cpf == null) {
			if (other.cpf != null)
				return false;
		} else if (!cpf.equals(other.cpf))
			return false;
		if (equipments == null) {
			if (other.equipments != null)
				return false;
		} else if (!equipments.equals(other.equipments))
			return false;
		if (exams == null) {
			if (other.exams != null)
				return false;
		} else if (!exams.equals(other.exams))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (level != other.level)
			return false;
		if (male != other.male)
			return false;
		if (maritalStatus != other.maritalStatus)
			return false;
		if (photo == null) {
			if (other.photo != null)
				return false;
		} else if (!photo.equals(other.photo))
			return false;
		if (postalCode == null) {
			if (other.postalCode != null)
				return false;
		} else if (!postalCode.equals(other.postalCode))
			return false;
		if (recoveryFactor != other.recoveryFactor)
			return false;
		if (schedules == null) {
			if (other.schedules != null)
				return false;
		} else if (!schedules.equals(other.schedules))
			return false;
		if (state == null) {
			if (other.state != null)
				return false;
		} else if (!state.equals(other.state))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "Athlete [id=" + id + ", firstName=" + firstName + ", lastName="
				+ lastName + ", cpf=" + cpf + ", address=" + address
				+ ", state=" + state + ", city=" + city + ", postalCode="
				+ postalCode + ", birthday=" + birthday + ", maritalStatus="
				+ maritalStatus + ", male=" + male + ", level=" + level
				+ ", recoveryFactor=" + recoveryFactor + ", user=" + user
				+ ", photo=" + photo + ", schedules=" + schedules
				+ ", equipments=" + equipments + ", exams=" + exams + "]";
	}



	
}
 
